package org.shinetour.sms.web.page;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description: 分页配置
 * @Author: ZhenHua Wang
 * @CreateDate: 2020/4/8 14:51
 */

public class PageHelper {

    /**
     * 当前记录起始索引
     */
    private static Long PAGE_NUM = 1L;
    /**
     * 每页显示记录数
     */
    private static Long PAGE_SIZE = 10L;
    /**
     * 排序列；默认iid
     */
    private static String ORDER_BY_COLUMN = "iid";

    /**
     * @Description: 组装分页查询参数
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/8 15:52
     */
    public static <T> Page<T> getPage(JSONObject req) {
        Long pageNum = req.getLong("pageNum", PAGE_NUM);
        Long pageSize = req.getLong("pageSize", PAGE_SIZE);
        Page<T> page = new Page<T>(pageNum, pageSize, true);

        String orderByColumn = req.getStr("orderByColumn", PageHelper.ORDER_BY_COLUMN);
        String sortRule = req.getStr("sortRule");
        if (!StrUtil.isEmpty(sortRule) && sortRule.equals("asc")) {
            page.setAsc(StrUtil.toUnderlineCase(orderByColumn));
        } else {
            page.setDesc(StrUtil.toUnderlineCase(orderByColumn));
        }
        return page;
    }

    /**
     * @Description: 组装分页查询参数
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/8 15:52
     */
    public static <T> Page<T> getPage(Map<String, String> map) {
        long pageNum = Long.parseLong(map.getOrDefault("pageNum", PAGE_NUM.toString()));
        long pageSize = Long.parseLong(map.getOrDefault("pageSize", PAGE_SIZE.toString()));
        Page<T> page = new Page<T>(pageNum, pageSize);

        String orderByColumn = map.getOrDefault("orderByColumn", PageHelper.ORDER_BY_COLUMN);
        String sortRule = map.getOrDefault("sortRule", "desc");
        if (!StrUtil.isEmpty(sortRule) && sortRule.equals("asc")) {
            page.setAsc(StrUtil.toUnderlineCase(orderByColumn));
        } else {
            page.setDesc(StrUtil.toUnderlineCase(orderByColumn));
        }
        return page;
    }

    /**
     * @Description: 组装分页查询
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/9 14:49
     */
    public static <T> Page<T> getPage() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if (StrUtil.isEmpty(pageNum)) {
            pageNum = PAGE_NUM.toString();
        }
        if (StrUtil.isEmpty(pageSize)) {
            pageSize = PAGE_SIZE.toString();
        }
        Page<T> page = new Page<T>(Long.parseLong(pageNum), Long.parseLong(pageSize), true);

        String orderByColumn = "";
        if (StrUtil.isEmpty(request.getParameter("orderByColumn"))) {
            orderByColumn = ORDER_BY_COLUMN;
        }
        String sortRule = request.getParameter("sortRule");
        if (!StrUtil.isEmpty(sortRule) && sortRule.equals("asc")) {
            page.setAsc(StrUtil.toUnderlineCase(orderByColumn));
        } else {
            page.setDesc(StrUtil.toUnderlineCase(orderByColumn));
        }
        return page;
    }

}
